package com.ruoyi.energy.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.energy.domain.NyEnteriseLoad;
import com.ruoyi.energy.domain.NyTransCheck;
import com.ruoyi.energy.domain.NyTransPart;
import com.ruoyi.energy.domain.NyTransPartArrs;
import com.ruoyi.energy.domain.vo.NyTransPartArrsWXVO;
import com.ruoyi.energy.mapper.NyTransCheckMapper;
import com.ruoyi.energy.mapper.NyTransPartArrsMapper;
import com.ruoyi.energy.mapper.NyTransPartMapper;
import com.ruoyi.energy.mapper.QyEnterpriseMapper;
import com.ruoyi.energy.service.INyTransPartArrsService;
import com.ruoyi.energy.utils.POJOConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 变压器部件属性Service业务层处理
 *
 * @author cjie
 * @date 2021-10-11
 */
@Service
public class NyTransPartArrsServiceImpl implements INyTransPartArrsService {
    @Autowired
    private NyTransPartArrsMapper nyTransPartArrsMapper;

    @Autowired
    private QyEnterpriseMapper qyEnterpriseMapper;

    @Autowired
    private NyTransCheckMapper nyTransCheckMapper;

    @Autowired
    private NyTransPartMapper nyTransPartMapper;
    /**
     * 查询变压器部件属性
     *
     * @param attrId 变压器部件属性ID
     * @return 变压器部件属性
     */
    @Override
    public NyTransPartArrs selectNyTransPartArrsById(Long attrId) {
        return nyTransPartArrsMapper.selectNyTransPartArrsById(attrId);
    }

    /**
     * 查询变压器部件属性列表
     *
     * @param nyTransPartArrs 变压器部件属性
     * @return 变压器部件属性
     */
    @Override
    public List<NyTransPartArrs> selectNyTransPartArrsList(NyTransPartArrs nyTransPartArrs) {
        //拼装企业及申报容量
        List<NyTransPartArrs> results = nyTransPartArrsMapper.selectNyTransPartArrsList(nyTransPartArrs);
//        for(NyTransPartArrs arr:results){
//            List<NyEnteriseLoad> nyEnteriseLoads = nyTransPartArrsMapper.selectNyEnteriseLoadByAttrId(arr.getAttrId());
//            if(nyEnteriseLoads != null) {
//                if (nyEnteriseLoads.size() > 1) {
//                    StringBuilder ens = new StringBuilder();
//                    StringBuilder enAllows = new StringBuilder();
//                    for (NyEnteriseLoad load : nyEnteriseLoads) {
//                        ens.append(load.getEntepriseName() + "/");
//                        enAllows.append(load.getApprovalLoad() + "/");
//                    }
//                    arr.setEn(ens.deleteCharAt(ens.length()-1).toString());
//                    arr.setEnAllow(enAllows.deleteCharAt(enAllows.length()-1).toString());
//                } else if (nyEnteriseLoads.size() == 1) {
//                    arr.setEn(nyEnteriseLoads.get(0).getEntepriseName());
//                    arr.setEnAllow(nyEnteriseLoads.get(0).getApprovalLoad().toString());
//                }
//            }
//        }
        return results;
    }

    /**
     * 新增变压器部件属性
     *
     * @param nyTransPartArrs 变压器部件属性
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNyTransPartArrs(NyTransPartArrs nyTransPartArrs) {
        //nyTransPartArrs.setAttrId(IdUtil.getSnowflake(0, 0).nextIdStr());
        nyTransPartArrs.setCreateTime(DateUtils.getNowDate());
        nyTransPartArrs.setCreateBy(SecurityUtils.getUsername());
        int rows = nyTransPartArrsMapper.insertNyTransPartArrs(nyTransPartArrs);
        insertNyEnteriseLoad(nyTransPartArrs);
        return rows;
    }

    /**
     * 修改变压器部件属性
     *
     * @param nyTransPartArrs 变压器部件属性
     * @return 结果
     */
    @Override
    public int updateNyTransPartArrs(NyTransPartArrs nyTransPartArrs) {
        nyTransPartArrs.setUpdateTime(DateUtils.getNowDate());
        nyTransPartArrs.setUpdateBy(SecurityUtils.getUsername());
        nyTransPartArrsMapper.deleteNyEnteriseLoadByAttrId(nyTransPartArrs.getAttrId());
        insertNyEnteriseLoad(nyTransPartArrs);
        return nyTransPartArrsMapper.updateNyTransPartArrs(nyTransPartArrs);
    }

    /**
     * 批量删除变压器部件属性
     *
     * @param attrIds 需要删除的变压器部件属性ID
     * @return 结果
     */
    @Override
    public int deleteNyTransPartArrsByIds(Long[] attrIds) {
        nyTransPartArrsMapper.deleteNyEnteriseLoadByAttrIds(attrIds);
        return nyTransPartArrsMapper.deleteNyTransPartArrsByIds(attrIds);
    }

    /**
     * 删除变压器部件属性信息
     *
     * @param attrId 变压器部件属性ID
     * @return 结果
     */
    @Override
    public int deleteNyTransPartArrsById(Long attrId) {
        nyTransPartArrsMapper.deleteNyEnteriseLoadByAttrId(attrId);
        return nyTransPartArrsMapper.deleteNyTransPartArrsById(attrId);
    }

    @Override
    public List<NyEnteriseLoad> selectNyEnteriseLoadList(NyEnteriseLoad nyEnteriseLoad) {
        return nyTransPartArrsMapper.selectNyEnteriseLoadList(nyEnteriseLoad);
    }

    @Override
    public List<NyTransPartArrs> getAirListByTransId(Long id) {
        NyTransPartArrs nyTransPartArrs = new NyTransPartArrs();
        nyTransPartArrs.setTransId(id);
        List<NyTransPartArrs> result = nyTransPartArrsMapper.selectNyTransPartArrsList(nyTransPartArrs);
        result.forEach(item->{
            item.setNyEnteriseLoadList(nyTransPartArrsMapper.selectNyEnteriseLoadByAttrId(item.getAttrId()));
        });
        return result;
    }

    @Override
    public List<NyTransPartArrsWXVO> getAirListByTransIdWX(Long id) {
        List<NyTransPartArrs> list = this.getAirListByTransId(id);
        return POJOConverter.copyList(list,NyTransPartArrsWXVO.class);
    }

    @Override
    public Map<String, List<NyEnteriseLoad>> getNyEnteriseLoadsAir( NyTransCheck nyTransCheck) {
        NyTransPartArrs nyTransPartArrs = this.selectNyTransPartArrsById(nyTransCheck.getSwiftId());
        Date checkTime = nyTransCheck.getCheckTime();
        List<NyEnteriseLoad> enteriseLoads = nyTransPartArrs.getNyEnteriseLoadList();
        //查询数值，填充
        NyTransCheck search = new NyTransCheck();
        enteriseLoads.forEach(item->{
            search.setCheckTime(checkTime);
            search.setEnterprise(item.getId().toString());
            List<NyTransCheck> nyTransChecks = nyTransCheckMapper.selectNyTransCheckList(search);
            if(nyTransChecks !=null && nyTransChecks.size()>0){
                item.setCurValue(nyTransChecks.get(0).getValue());
            }
        });
        return enteriseLoads.stream().collect(Collectors.groupingBy(NyEnteriseLoad::getEntepriseName));
    }

    @Override
    public Map<String, List<NyEnteriseLoad>> genNyEnteriseLoadsTrans(NyTransCheck nyTransCheck) {
        List<NyTransPartArrs> nyTransPartArrsList = this.getAirListByTransId(nyTransCheck.getTransId());
        Date checkTime = nyTransCheck.getCheckTime();
        List<NyEnteriseLoad> enteriseLoads = new ArrayList<>();
        for(NyTransPartArrs air:nyTransPartArrsList){
            if(air.getNyEnteriseLoadList() != null){
                enteriseLoads.addAll(air.getNyEnteriseLoadList());
            }
        }
        //查询数值，填充
        NyTransCheck search = new NyTransCheck();
        enteriseLoads.stream().forEach(item->{
            search.setCheckTime(checkTime);
            search.setEnterprise(item.getId().toString());
            List<NyTransCheck> nyTransChecks = nyTransCheckMapper.selectNyTransCheckList(search);
            if(nyTransChecks !=null && nyTransChecks.size()>0){
                item.setCurValue(nyTransChecks.get(0).getValue());
                item.setCheckTime(checkTime);
            }
        });
        return enteriseLoads.stream().collect(Collectors.groupingBy(NyEnteriseLoad::getEntepriseName));
    }

    @Override
    public NyTransPartArrs selectNyTransPartArrsByAirId(Long id) {
        return nyTransPartArrsMapper.selectNyTransPartArrsByAirId(id);
    }

    /**
     * 新增${subTable.functionName}信息
     *
     * @param nyTransPartArrs 变压器部件属性对象
     */
    public void insertNyEnteriseLoad(NyTransPartArrs nyTransPartArrs) {
        List<NyEnteriseLoad> nyEnteriseLoadList = nyTransPartArrs.getNyEnteriseLoadList();
        Long attrId = nyTransPartArrs.getAttrId();
        if (StringUtils.isNotNull(nyEnteriseLoadList)) {
            List<NyEnteriseLoad> list = new ArrayList<NyEnteriseLoad>();
            StringBuilder ens = new StringBuilder();
            StringBuilder enAllows = new StringBuilder();
            for (NyEnteriseLoad nyEnteriseLoad : nyEnteriseLoadList){
                nyEnteriseLoad.setAttrId(attrId);
//                if(StringUtils.isNotNull(nyEnteriseLoad.getEntepriseId())){
//                    QyEnterprise qyEnterprise = qyEnterpriseMapper.selectQyEnterpriseById(nyEnteriseLoad.getEntepriseId());
//                    nyEnteriseLoad.setEntepriseName(qyEnterprise.getEnterpriseName());
//                    list.add(nyEnteriseLoad);
//                }
                ens.append(nyEnteriseLoad.getEntepriseName() + "/");
                enAllows.append(nyEnteriseLoad.getApprovalLoad() + "/");
                list.add(nyEnteriseLoad);
            }
            nyTransPartArrs.setEn(ens.deleteCharAt(ens.length()-1).toString());
            nyTransPartArrs.setEnAllow(enAllows.deleteCharAt(enAllows.length()-1).toString());
            if (list.size() > 0) {
                nyTransPartArrsMapper.batchNyEnteriseLoad(list);
                nyTransPartArrsMapper.updateNyTransPartArrs(nyTransPartArrs);
                NyTransPart part = new NyTransPart();
                part.setId(nyTransPartArrs.getAirId());
                part.setUser(nyTransPartArrs.getEn());
                nyTransPartMapper.updateNyTransPart(part);
            }
        }
    }
}
